import Vue from "vue";
import App from "./App";
import FastClick from "fastclick";
import router from "./router";
import config from "./config/config.js";
import store from "./store/store.js";
// import VueAnalytics from 'vue-analytics'
import "./filters/filters";

import MintUI from 'mint-ui';
import 'mint-ui/lib/style.css';


Vue.use(MintUI);
import Vant from 'vant';
import 'vant/lib/vant-css/index.css';

Vue.use(Vant);
import { Sku } from 'vant';

Vue.use(Sku);

import Swiper from 'swiper';
import 'swiper/dist/css/swiper.min.css';
import wxshare from "./plugins/wxshare";
import isCopy from "./plugins/isCopy";
import JsEncrypt from "./plugins/JsEncrypt";
import vuexI18n from "vuex-i18n";
Vue.use(vuexI18n.plugin, store);
import objectAssign from "object-assign";
import axios from 'axios'
// Vue.prototype.axios = axios 组件调用this.axios.get(...)
// Vue.prototype.$ajax = axios  换个名字 组件调用this.$ajax.get(...)
window.axios = axios;  //组件中调用 axios.get(...)

const vuxLocales = require("json-loader!yaml-loader!./locales/all.yml");
const componentsLocales = require("json-loader!yaml-loader!./locales/components.yml");

const finalLocales = {
  en: objectAssign(vuxLocales["en"], componentsLocales["en"]),
  "zh-CN": objectAssign(vuxLocales["zh-CN"], componentsLocales["zh-CN"])
};

for (let i in finalLocales) {
  Vue.i18n.add(i, finalLocales[i]);
}

import VueAwesomeSwiper from "vue-awesome-swiper";
import "swiper/dist/css/swiper.css";

import VueScroller from "vue-scroller";
Vue.use(VueScroller);

import VueClipboards from "vue-clipboards";
Vue.use(VueClipboards);

import CountDown from "vue2-countdown";
Vue.use(CountDown);

import VueStomp from "vue-stomp";
Vue.use(VueStomp);

import Progress from "vue-multiple-progress";
Vue.component("vprogress", Progress);
Vue.use(Progress);
import { ConfigPlugin, LocalePlugin } from "vux";

Vue.use(LocalePlugin);
const nowLocale = localStorage["lang"] || "zh-CN";
Vue.i18n.set(nowLocale);

import {
  ToastPlugin,
  LoadingPlugin,
  ConfirmPlugin,
  AlertPlugin,
  cookie,
  CloseDialogsPlugin,
  WechatPlugin
} from "vux";
// Vue.use(CloseDialogsPlugin, router)
Vue.use(ConfirmPlugin);
Vue.use(LoadingPlugin);
Vue.use(ToastPlugin);
Vue.use(AlertPlugin);
Vue.use(WechatPlugin);

Vue.use(VueAwesomeSwiper /* { default global options } */);

FastClick.attach(document.body);
Vue.config.productionTip = false;

Vue.use(require("vue-wechat-title"));

router.beforeEach((to, from, next) => {
  var navs = ["/home", "/market", "/find", "/items", "/mine"];
  var index = navs.indexOf(to.path);
  if (index != -1) {
    store.commit("change", {
      tar: index
    });

    if (window.localStorage.readme) {
      next();
      return;
    } else {
      // next({
      //   path: "/rules"
      // });
      next();
      return;
    }
  }
   if (to.meta.skipAuth) {
    next();
    
  } 

  if (to.meta.requireAuth) {
    if (window.localStorage.token_2) {
      next();
    } else {
      next({
        path: "/login"
      });
    }
  } else {
    next();
  }
});

router.afterEach(function(to) {
  if (window.ga) {
    var userid = cookie.get("_gb");
    if (userid == undefined) {
      userid = "nobody";
    }
    window.ga("set", "page", to.fullPath); // 你可能想根据请求参数添加其他参数，可以修改这里的 to.fullPath
    window.ga("set", "userId", userid); // 使用已登录的 user_id 来设置用户 ID。
    window.ga("send", "pageview");
  }
});
/* eslint-disable no-new */
new Vue({
  store,
  router,
  render: h => h(App)
}).$mount("#app-box");
